package com.treasure.mall.biz.service.core.marketing.impl;

import com.treasure.mall.biz.base.AbstractSearchableCrudService;
import com.treasure.mall.biz.base.SpecificationAndSort;
import com.treasure.mall.biz.bo.config.SignRewardConfig;
import com.treasure.mall.biz.dto.BaseSaveDTO;
import com.treasure.mall.biz.dto.BaseSearchDTO;
import com.treasure.mall.biz.dto.user.ModifyIntegralDTO;
import com.treasure.mall.biz.entity.marketing.UserSignInPO;
import com.treasure.mall.biz.repository.marketing.UserSignInRepository;
import com.treasure.mall.biz.service.core.BaseConfigService;
import com.treasure.mall.biz.service.core.marketing.UserSignInService;
import com.treasure.mall.biz.service.core.user.UserService;
import com.treasure.mall.zbq.common.base.ApiResult;
import com.treasure.mall.zbq.common.exception.ApiException;
import com.treasure.mall.zbq.common.ienum.integral.IntegralEnums;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * @author allan
 * @version 1.0.0
 * @date 2020/8/22
 */
@Service
public class UserSignInServiceImpl extends AbstractSearchableCrudService<UserSignInPO, Long> implements UserSignInService {
    private UserSignInRepository signInRepository;
    @Resource
    private UserService userService;
    @Resource
    private BaseConfigService baseConfigService;

    @Autowired
    protected UserSignInServiceImpl(UserSignInRepository repository) {
        super(repository);
        this.signInRepository = repository;
    }

    @Override
    protected SpecificationAndSort<UserSignInPO> specificationAndSort(BaseSearchDTO searchDTO) {
        return null;
    }

    @Override
    @Transactional
    public UserSignInPO signIn(Long userId) {
        boolean signed = signInRepository.existsByUserIdAndSignDate(userId, LocalDate.now());

        if (signed) {
            throw new ApiException(ApiResult.error("今日已签到"));
        }

        UserSignInPO userSignInPO = new UserSignInPO();
        userSignInPO.setUserId(userId);
        userSignInPO.setSignDate(LocalDate.now());
        UserSignInPO lastPO = signInRepository.findByUserIdAndSignDate(userId, userSignInPO.getSignDate().minusDays(1));
        if (lastPO == null) {
            //断签
            userSignInPO.setContinueNum(1);
        } else {
            if (lastPO.getContinueNum() == 7) {
                //从第一天开始
                userSignInPO.setContinueNum(1);
            } else {
                userSignInPO.setContinueNum(lastPO.getContinueNum() + 1);
            }
        }
        //得到签到获得积分的配置
        SignRewardConfig config = baseConfigService.getSignConfig().getConfigByDay(userSignInPO.getContinueNum());
        userSignInPO.setIntegral(config.getRewardIntegral());

        // 增加积分
        userService.modifyIntegral(
                ModifyIntegralDTO.builder()
                        .userId(userId)
                        .integral(userSignInPO.getIntegral())
                        .logType(IntegralEnums.LogType.SIGN_IN)
                        .build()
        );

        return save(userSignInPO);
    }

    @Override
    public List<UserSignInPO> logs(Long userId) {
        LocalDate begin = LocalDate.now().minusDays(3);
        LocalDate end = LocalDate.now().plusDays(3);

        return signInRepository.findByUserIdAndSignDateBetween(userId, begin, end);
    }

    @Override
    public UserSignInPO findByDate(Long userId, LocalDate date) {
        return signInRepository.findByUserIdAndSignDate(userId, date);
    }

    @Override
    public UserSignInPO findLast(Long userId) {
        return signInRepository.findTopByUserIdAndSignDateBeforeOrderBySignDateDesc(userId, LocalDate.now());
    }

    @Override
    public UserSignInPO save(BaseSaveDTO saveDTO) {
        return null;
    }
}
